
package petrivis.view;

import java.awt.geom.Rectangle2D;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.Action;
import prefuse.util.GraphicsLib;
import prefuse.util.display.DisplayLib;

/**
 * Action that Zooms until the graph fits in the display
 * @author Jorge Munoz
 */
public class ZoomToFitAction extends Action {

    /**
     * Create a new Action with a specified Visualization and zero duration.
     * @param vis the Visualization this Action should process. If this Action
     * is registered with another Visualization, this value will be overwritten.
     */
    public ZoomToFitAction(Visualization vis)
    {
        super();
        m_vis = vis;
    }

    /**
     * Runs the Action, Zooming unitl the Graph fits in the Display
     * @param frac the fraction of this Action's duration that has elapsed.
     */
    public void run(double frac)
    {
        //zoom to fit
        Rectangle2D bounds = m_vis.getBounds(Visualization.ALL_ITEMS);
        Display d = m_vis.getDisplay(0);
        GraphicsLib.expand(bounds, 50 + (int)(1/d.getScale()));
        DisplayLib.fitViewToBounds(d, bounds, 0);
    }

}
